# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=openpgl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.4.1
pkgrel=3
pkgdesc="Intel(R) Open Path Guiding Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://www.openpgl.org/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/OpenPathGuidingLibrary/openpgl/archive/v${pkgver}-beta.tar.gz"
        001-add-compile-flags.patch
        002-Fix-finding-TBB.patch
        003-Fix-building-on-mingw-w64-aarch64.patch)
sha256sums=('db63f5dac5cfa8c110ede241f0c413f00db0c4748697381c4fa23e0f9e82a754'
            'e9d6c2ad5df1c7b76ebb73fa59a77f478af1d14f72b0ef39a4c5a44cce04b7b1'
            'e89b72a670d877139baa125d845973fcffde2dcf26f65f01cddd58d6e10a9b7d'
            '59bba47e18d75a3d7e19f2e0eeca84f1b829b459b70aba0928ad28914bf8729e')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}-beta
  patch -p1 -i "${srcdir}"/001-add-compile-flags.patch
  patch -p1 -i "${srcdir}"/002-Fix-finding-TBB.patch
  # Backported from https://github.com/embree/embree/commit/20ef1bab
  patch -p1 -i "${srcdir}"/003-Fix-building-on-mingw-w64-aarch64.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -D_USE_MATH_DEFINES -w" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}-beta

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}-beta/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
